package com.lmb.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lmb.bean.Employee;
import com.lmb.bean.R;
import com.lmb.bean.Setmeal;
import com.lmb.dao.SetmealDao;
import com.lmb.dto.DishDto;
import com.lmb.dto.SetmealDto;
import com.lmb.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j
@RequestMapping("/setmeal")
@RestController
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    //新增
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){
        setmealService.saveWithDish(setmealDto);
        return R.success("新增成功！");
    }

    //分页查询
    @GetMapping("/page")
    public R<Page<SetmealDto>> page(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "10") Integer pageSize,String name){
        Page<SetmealDto> setmealDtoPage = setmealService.findPage(page, pageSize, name);
        return R.success(setmealDtoPage);
    }

    //菜品修改
    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto){
        //1.调用service处理
        setmealService.updateWithFlavors(setmealDto);
        //2.响应处理结果
        return R.success("修改成功！");
    }

    //删除
    @DeleteMapping
    public R<String> deleteByIds(@RequestParam List<Long> ids){
        log.info("SetmealController-deleteByIds：ids={}",ids);

        //1.调用service处理
        setmealService.deleteByIds(ids);

        //2.响应处理结果
        return R.success("删除成功！");
    }

    //根据id查询菜品信息
    @GetMapping("/{id}")
    public R<SetmealDto> getById(@PathVariable Long id){
        //1.调用service处理
        SetmealDto setmealDto = setmealService.getByIdWithFlavors(id);

        //2.响应处理结果
        return R.success(setmealDto);
    }

    //根据分类id查询套餐列表数据
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){
        //1.调用service处理
        //1.1：创建wrapper对象
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();

        //1.2：设置查询条件 分类id和status
        wrapper.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
        wrapper.eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus());

        //1.3：调用service中的list方法查询 传入wrapper对象
        List<Setmeal> list = setmealService.list(wrapper);
        //2.响应数据
        return R.success(list);
    }

    //启用禁用
    @PostMapping("/status/{status}")
    public R<String> updateEmp(@PathVariable Integer status,@RequestParam List<Long> ids){
        setmealService.updateSetmealStatus(status, ids);
        return R.success("修改成功！");
    }


}
